var token = localStorage.getItem('tok');
//输入卡号获取银行卡名称
$(".bankNo").change(function(){
    var bankNo=$('.bankNo').val();
    //请求接口
    $.ajax({
        type: "POST",
        url: $host+'/api/bankData ',
        async: true,
        dataType:'json',
        data:{
           "bankNo": bankNo,
        },
        success: function(result){
            var bankName=result.data.bankName;
            $('.bankName').text(bankName);
        },
        error:function(){
            console.log('获取接口错误');
        }
    });
});
//选择省
var list1=[];
var list1_len='';
var list2=[];
var list3=[];
var list3_len='';
var list4=[];
var str1="";
var str2="";
var a,b;
//获取省份
$.ajax({
    type: "GET",
    url: $host+'/api/open/common/getProvinceList',
    async: true,
    dataType:'json',
    success: function(result){
        var data=result.data.province;
        for(let i=0;i<data.length;i++){
            list1.push(data[i].name);
            list2.push(data[i].id);
        }
    },
    error:function(){
        console.log('获取接口错误');
    }
});
//省点击
$('.yhks').click(function(){
    $('.mask-show').show();
    listOne(list1,str1);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
        direction : 'vertical',
        height:40,
        initialSlide :1,
        on: {
            slideChangeTransitionEnd: function(){
                var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
                $('.yhks_a').text(a);
                list1_len=this.activeIndex//切换结束时，告诉我现在是第几个slide
            },
        },
        onDestroy: function(swiper){
            alert('你销毁了Swiper;');
        }
    })
    $('.fix_btn').click(function(){
        mySwiper.destroy(false)
    })
})
//市点击
$('.yhkkhs').click(function(){
    list3=[];
    list4=[];
    $('.mask-show').show();
    $.ajax({
        type: "GET",
        url: $host+`/api/open/common/getCityList/`+a,
        async: true,
        dataType:'json',
        success: function(result){
        var data=result.data.city;
        for(let i=0;i<data.length;i++){
            list3.push(data[i].name);
            list4.push(data[i].id);
        }
        listOne(list3,str2);
        timer();
        var mySwiper = new Swiper('.swiperone.swiper-container',{
                direction : 'vertical',
                height:40,
                initialSlide :1,
                on: {
                    slideChangeTransitionEnd: function(){
                        var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
                        $('.yhkkhs_a').text(a);
                        list3_len=this.activeIndex//切换结束时，告诉我现在是第几个slide
                    },
                },
                onDestroy: function(swiper){
                    alert('你销毁了Swiper;');
                }
            })
            $('.fix_btn').click(function(){
                mySwiper.destroy(false)
            })
    },
    error:function(){
        console.log('获取接口错误');
    }
})
})
//点击确定
$('.fix_btn').click(function(){
    $('.mask-show').hide();
    a = list2[list1_len];
    b = list4[list3_len];
})
function listOne(array,obj){
    let sw_height=array.length>=5 ? 5*1.65 : array.length*1.65
    $('.containers').height(sw_height+'rem');
    for(var i = 0;i<array.length;i++){
        obj += '<div class="swiper-slide">'+array[i]+'</div>'
    }
    $(".swiperone .swiper-wrapper").html(obj);
}
function timer(){
    setTimeout(function(){
        $(".swiperone").css("padding-top","40px");
    },50)
}

//点击确认绑定
$('.btn').click(function(){
    var bankNo=$('.bankNo').val();//银行卡号
    var bankName=$('.bankName').text();//银行名
    var branchName=$('.branchName').val();//支行信息
    var provinceId=a;//省ID
    var cityId=b;//城市ID
    var userName=$('.userName').val();//用户名
    var userPhone=$('.userPhone').val();//用户手机号
    var userCardNum=$('.userCardNum').val();//用户身份证
    if(bankNo==""||bankName==""||branchName==""||provinceId==""||cityId==""||userName==""||userPhone==""||userCardNum==""){
        layer.msg('请完善您的信息');
    }else{
        $.ajax({
            type: "POST",
            url: $host+'/api/app/bank/addBankInfo',
            async: true,
            dataType:'json',
            data:{
                token:token,
                "bankNo":bankNo,
                "bankName":bankName,
                "branchName":branchName,
                "provinceId":provinceId,
                "cityId":cityId,
                "userName":userName,
                "userPhone":userPhone,
                "userCardNum":userCardNum
            },
            success: function(result){
                console.log(result)
                if(result.status==900000){
                    $('.mask').show();
                    setTimeout(()=>{
                        $('.mask').hide();
                        window.location.href="shenhejd.html?token="+token
                    },2000);
                    $('.popup_img').attr('src', '../images/Page 1@2x.png');
                    $('.popup_p').text('绑定银行卡成功！');
                }else{
                        $('.mask').show();
                        setTimeout(()=>{
                            $('.mask').hide();
                        },2000);
                        $('.popup_img').attr('src', '../images/Group@2x.png');
                        $('.popup_p').text('绑定银行卡失败！')
                }
            },
            error:function(){
                console.log('获取接口错误');
            }
       });
    }
})
//点击返回
$('.bdyhkfh').click(function(){
    window.location.href="gerenzx.html?token="+token;
})